package io.binghe.im.platform.friend.application.event;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.event.EventHandler;
import com.alibaba.cola.event.EventHandlerI;
import com.alibaba.fastjson.JSON;
import io.binghe.im.platform.friend.application.cache.FriendCacheService;
import io.binghe.im.platform.friend.domain.event.IMFriendEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

/**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description 基于Cola的用户事件处理器
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
@EventHandler
@ConditionalOnProperty(name = "message.mq.event.type", havingValue = "cola")
public class IMFriendColaEventHandler implements EventHandlerI<Response, IMFriendEvent> {

    private final Logger logger = LoggerFactory.getLogger(IMFriendColaEventHandler.class);

    @Autowired
    private FriendCacheService friendCacheService;

    @Override
    public Response execute(IMFriendEvent imFriendEvent) {
        if (imFriendEvent == null || imFriendEvent.getId() == null){
            logger.info("cola|friendEvent|接收好友事件参数错误");
            return Response.buildSuccess();
        }
        logger.info("cola|friendEvent|接收好友事件|{}", JSON.toJSON(imFriendEvent));
        friendCacheService.updateFriendCache(imFriendEvent);
        return Response.buildSuccess();
    }
}
